﻿@page "/admin/post"
@using Moonglade.Web.PagedList
@model Moonglade.Web.Pages.Admin.PostModel
@{
    ViewBag.Title = "Posts";
}

@section scripts {
    <script type="module">
        import * as utils from '/js/app/utils.module.js'

        window.deleteConfirm = function (postid) {
            var cfm = confirm("Delete Confirmation?");
            if (cfm) {
                window.deletePost(postid);
            }
        }

        window.deletePost = function (postid) {
            callApi(`/api/post/${postid}/recycle`,
                'DELETE',
                {},
                (resp) => {
                    const postElement = document.getElementById(`post-${postid}`);
                    if (postElement) {
                        postElement.remove();
                    }
                });
        }

        utils.formatUtcTime();
    </script>
}

@section admintoolbar {
    <partial name="_PostManageHeader" />
}

@Html.AntiForgeryToken()

<div>
    <form method="post">
        <div class="d-flex search-box mb-3">
            <input type="search" aria-label="Search" asp-for="SearchTerm" class="form-control" placeholder="@SharedLocalizer["Search"]" />
            <button class="btn btn-light btn-search my-sm-0" type="submit"><i class="bi-search text-dark"></i></button>
        </div>
    </form>

    @if (Model.PostSegments.Count == 0)
    {
        <partial name="_NoPosts" />
    }
    else
    {
        foreach (var item in Model.PostSegments)
        {
            <div id="post-@item.Id" class="admin-post-entry p-3 mb-2 rounded-3 shadow-sm border">
                <div class="row">
                    <div class="col">
                        <a asp-page="/Post"
                           asp-route-year="@item.PubDateUtc.GetValueOrDefault().Year"
                           asp-route-month="@item.PubDateUtc.GetValueOrDefault().Month"
                           asp-route-day="@item.PubDateUtc.GetValueOrDefault().Day"
                           asp-route-slug="@item.Slug"
                           class="d-inline-block"
                           title="@item.Slug"
                           target="_blank">
                            <h6>
                                @item.Title
                                @if (item.IsFeatured)
                                {
                                    <i class="bi-star-fill text-warning"></i>
                                }

                                @if (item.IsOutdated)
                                {
                                    <span class="badge rounded-pill text-bg-secondary ms-2">Outdated</span>
                                }
                            </h6>
                        </a>
                        <div class="mb-2">
                            @item.ContentAbstract
                        </div>
                        <div>
                            <i class="bi-calendar-event"></i>
                            <span class="text-muted me-2">
                                <time data-utc-label="@item.CreateTimeUtc.ToString("u")">@item.CreateTimeUtc</time>
                            </span>
                        </div>
                    </div>

                    <div class="col-auto">
                        <a class="btn btn-outline-accent btn-sm btn-edit" asp-page="./EditPost" asp-route-id="@item.Id"><i class="bi-pen"></i></a>
                        <button class="btn btn-outline-danger btn-sm btn-delete" onclick="window.deleteConfirm('@item.Id')"><i class="bi-trash"></i></button>
                    </div>
                </div>
            </div>
        }

        @Html.PagedListPager(Model.PostSegments, page => $"?pageIndex={page}&searchTerm={Model.SearchTerm}", new()
{
    UlElementClasses = new[] { "pagination mt-4" }
})
    }
</div>